<template>
  <div>
    <div class="back">
      <router-link to="/login">
        <img :src="backSrc" alt="" />
      </router-link>
    </div>
    <div class="login">
      <h2>用户注册</h2>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="user">
          <el-input v-model.number="ruleForm.user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item class="submit">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    var checkUser = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        user: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        user: [{ validator: checkUser, trigger: "blur" }],
      },
      backSrc: require("../assets/images/back.png"),
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log(this.ruleForm);
          this.creatAccount(this.ruleForm);
        } else {
          console.log("注册失败!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    creatAccount(mes) {
      let userName = mes.user;
      let passWord = mes.pass;
      let data = { userName, passWord };
      console.log(data);
      axios
        .post("http://127.0.0.1/list/add", data, {
          headers: "application/x-www-form-urlencoded",
        })
        .then((res) => {
          console.log(res);
          let {data} = res;
          if(data.code == -1){
            alert("注册失败!!");
          } 
          if(data.code == 200){
            alert("注册成功!");
            this.$router.push('/login');
          }
        });
    },
  },
};
</script>


<style lang="less" scoped>
.login {
  width: 90vw;
  margin: 0 auto;
}
.back {
  height: 54px;
  background-color: #fff;
  img {
    display: block;
    width: 40px;
    height: 40px;
    padding-top: 7px;
  }
}
h2 {
  margin: 40px;
}
</style>
<style lang="less">
.el-form {
  margin-left: -30px;
}
.submit {
  margin-left: -50px !important;
}
</style>